package com.itcg.springboot03deptemptest.service.impl;


import com.itcg.springboot03deptemptest.mapper.EmpMapper;
import com.itcg.springboot03deptemptest.pojo.Emp;
import com.itcg.springboot03deptemptest.pojo.EmpC;
import com.itcg.springboot03deptemptest.pojo.Page;
import com.itcg.springboot03deptemptest.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;


@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    @Transactional
    public Emp updates(Emp emp) {
        return empMapper.updates(emp);
    }

    @Override
    @Transactional
    public void deletes(Integer[] ids) {
        empMapper.deletes(ids);
    }

    @Override
    public Page<Emp> findByCondition(EmpC empC, Integer pageNum, Integer pageSize) {
        String name = empC.getName();
        name = "%" + name + "%";
        Page<Emp> empPage = new Page<>();
        //总页数
        int totalPage = (int) Math.ceil((pageNum * 1.0 - 1) / pageSize);
        if (totalPage <= 0) {
            totalPage = 1;
        }
        //获取数据
        List<Emp> empList = empMapper.findByCondition(empC, (pageNum - 1) * pageSize, pageSize);
        Long totalCount = (long) empList.size();
        //设置总记录数
        empPage.setTotalCount(totalCount);
        //设置每页显示记录数
        empPage.setPageSize(pageSize);
        //设置当前页面
        empPage.setPageNum(pageNum);
        //设置总页数
        empPage.setTotalPage(totalPage);
        //设置当前页面数据
        empPage.setDataList(empList);

        return empPage;
    }

    @Override
    public Emp findById(Integer id) {
        return empMapper.findById(id);
    }

    @Override
    @Transactional
    public int inserts(Emp emp) {
        return empMapper.inserts(emp);
    }
}
